from django.contrib.auth import login
from django.shortcuts import render, redirect

# Create your views here.
from django.urls import reverse

from apps.oauth.models import OAuthQQUser
from apps.oauth.utils import serect_openid, check_openid
from apps.users.models import User

'''
我们所做的这些都是为了 openid
openid是次网站上唯一对应用户身份的表示
网站可以将此ID进行存储便于下次登陆时，辨识其身份

1. 生成appid 和 appkey,redirect_uri

        QQ_CLIENT_ID = '101518219'

        QQ_CLIENT_SECRET = '418d84ebdc7241efb79536886ae95224'

        QQ_REDIRECT_URI = 'http://www.meiduo.site:8000/oauth_callback'
2. 放置一个按钮
3. 按钮点击可以跳转到指定页面
        读文档
4. 跳转到指定页面需要用户同意登陆，同意登陆之后，腾讯会给我们一个code

5. 用code 换取 token

6. 用 token 换取 openid

'''
'''
openid  和 用户id  一一对应


'''
from django.http import HttpResponseBadRequest
from  django.views import View

class QQLoginView(View):

    def get(self,request):
        # 1.获取code
        code = request.GET.get("code")
        state = request.GET.get("state")
        if code is None:
            return HttpResponseBadRequest("没有code")
        # 2.通过code换取token

        # 2.1 导入 QQLoginTool
        from meiduo_mall import settings
        from QQLoginTool.QQtool import OAuthQQ

        # 2.2 创建实例对象
        # client_id = None, client_secret = None, redirect_uri = None, state = None
        oauthqq = OAuthQQ(client_id=settings.QQ_CLIENT_ID,              #app_key
                          client_secret=settings.QQ_CLIENT_SECRET,
                          redirect_uri=settings.QQ_REDIRECT_URI,
                          state=state)

        # 2.3 获取token
        token = oauthqq.get_access_token(code)


        # 3.通过token 换取openid
        openid = oauthqq.get_open_id(token)

        # 4.根据openid 进行数据 查询判断
        try:
            qquser = OAuthQQUser.objects.get(openid=openid)
        except OAuthQQUser.DoesNotExist:
            # 若不存在
            new_openid = serect_openid(openid)

            return render(request, "oauth_callback.html",context={"openid":new_openid})





        else:
            # 若存在
            # 保持登陆的状态
            login(request,qquser.user)

            response = redirect(reverse("contents:index"))

            # 设置cookie
            response.set_cookie("username",qquser.user.username,max_age=24*3600)

        return render(request,"oauth_callback.html")


    def post(self,request):
        # ①接收数据
        mobile = request.POST.get("mobile")
        pwd = request.POST.get("pwd")
        sms_code = request.POST.get("sms_code")
        secret_openid = request.POST.get("openid")


        # 添加解密
        openid = check_openid(secret_openid)
        if openid is None:
            return HttpResponseBadRequest("openid  error...")


        # ②验证数据  openid    (老师省略)
        #     参数是否齐全
        #     手机号是否符合规则
        #     密码是否符合规则
        #     短信验证码是否一致

        # ③根据手机号进行用户信息的查询  user
        try:
            user = User.objects.get(mobile=mobile)
        except  User.DoesNotExist:
            # 如果不存在,说明用户手机号没有注册过,我们就以这个手机号注册一个用户
            user = User.objects.create_user(username=mobile,
                                            password=pwd,
                                            mobile=mobile)

        else:
        #     如果存在,则需要验证密码
            if not user.check_password(pwd):
                return HttpResponseBadRequest("密码错误")

        # ④ 绑定openid 和 user
        OAuthQQUser.objects.create(user=user,openid=openid)
        # ⑤ 登陆(设置登陆状态,设置cookie,跳转到首页)
        login(request,user)

        response = redirect(reverse("contents:index"))

        response.set_cookie("username",user.username,max_age=24*3600)

        return response




















